import { Controller, Get, Post, Body, Put, Param, Delete } from '@nestjs/common'
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger'
import { EmployeeClass, EmployeeModel } from './entities/employee.entity'

@ApiBearerAuth()
@Controller('employee')
@ApiTags('员工管理')
export class EmployeeController {
  @Get()
  @ApiOperation({
    summary: '员工列表',
    description: '显示全部员工列表',
  })
  async findAll() {
    return EmployeeModel.find().select('-__v')
  }

  @Get(':id')
  @ApiOperation({
    summary: '员工详情',
    description: '根据id获取一条员工详情',
  })
  async detail(@Param('id') id: string) {
    return await EmployeeModel.findById<EmployeeClass>(id).select('-__v')
  }

  @Post()
  @ApiOperation({
    summary: '创建详情',
    description: '创建一条员工详情',
  })
  async create(@Body() createEmployee: EmployeeClass) {
    await EmployeeModel.create(createEmployee)
    return {
      success: true,
      message: '员工创建成功！',
    }
  }

  @Put(':id')
  @ApiOperation({
    summary: '更新详情',
    description: '根据id编辑一条员工详情',
  })
  async update(@Param('id') id: string, @Body() updateEmployee: EmployeeClass) {
    await EmployeeModel.findByIdAndUpdate(id, updateEmployee)
    return {
      success: true,
      message: '员工更新成功！',
    }
  }

  @Delete(':id')
  @ApiOperation({
    summary: '删除',
    description: '根据id删除一条员工详情',
  })
  async remove(@Param('id') id: string) {
    await EmployeeModel.findByIdAndDelete(id)
    return {
      success: true,
      message: '员工删除成功！',
    }
  }
}
